package cn.itcast.controller;

import cn.itcast.domain.Admin;
import cn.itcast.domain.PageBean;
import cn.itcast.domain.Room;
import cn.itcast.service.AdminService;
import cn.itcast.service.RoomService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoomService roomService;
    /**
     * 模糊查询
     */


    /**
     * 管理管登陆
     */
    @RequestMapping("/login")
    public String login(String username,String password, HttpServletRequest request) {
        //接收用户名和密码html
        String adminUsername = request.getParameter("adminUsername");
        String adminPassword = request.getParameter("adminPassword");
        System.out.println(adminUsername);
        System.out.println(adminPassword);
        Admin admin_html =new Admin();
        admin_html.setAdminUsername(adminUsername);
        admin_html.setAdminPassword(adminPassword);
        Admin admin1=adminService.adminLogin(adminUsername,adminPassword);
        //接收用户名和密码jsp
        //Admin admin1 = adminService.adminLogin(username,password);
        System.out.println(admin1);
        if (admin1!=null) {
            //将admin存入session域
            request.getSession().setAttribute("admin",admin1);
            //将查询到的所有admin放到域中
            List<Admin> admins = adminService.findAdmin();
            request.getSession().setAttribute("admins",admins);
            //将查询到的房间信息存入session
            List<Room> rooms = roomService.findAll(request);
            request.getSession().setAttribute("rooms",rooms);
            //查询所有房间信息将房间总数放入session
            List<Room> roomList = roomService.findAll(request);
            request.getSession().setAttribute("roomNumber",roomList.size());
            //查询空房间数量放入session将空房间存入空集合
            List<Room>emptyRooms=new ArrayList<>();
            int emptyRoom=0;
            for (Room room : roomList) {
                if ("空".equals(room.getRoomFlag())){
                    emptyRoom+=1;
                    emptyRooms.add(room);
                }
            }
            request.getSession().setAttribute("emptyRoom",emptyRoom);
            request.getSession().setAttribute("emptyRooms",emptyRooms);
            //查询满房间数量放入session
            int fullRoom=roomList.size()-emptyRoom;
            request.getSession().setAttribute("fullRoom",fullRoom);
            //如果查询到了用户那么跳转到首页(展示房间页面)
            if(admin1.getAdminFlag()==null||admin1.getAdminFlag().equals("")){
                return "redirect:/pages/room-list.jsp";
            }else{
                return "redirect:/pages/room-list2.jsp";
            }
        }
        //若失败则为空
        return "login_failed";
    }

    /**
     * 查询所有管理员
     *
     * @return 给前台返回查询的结果
     */
    @RequestMapping("/findAll")
    public ModelAndView findAll(ModelAndView modelAndView,HttpServletRequest request) {
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        if(rows==null||"".equals(rows)){
            rows="5";
        }
        PageBean pageBean=adminService.findAdmin(currentPage,rows);
        List<Admin> admins = adminService.findAdmin();
        request.getSession().setAttribute("admins",admins);
        modelAndView.addObject("pageBean",pageBean);
        modelAndView.setViewName("admin_list2");
        return modelAndView;
    }

    /**
     * 新增管理员
     */
    @RequestMapping("/save")

    public String save(Admin admin) {
        System.out.println(111);
        adminService.adminSave(admin);
        return "redirect:/admin/findAll";
    }
    @RequestMapping("/退出")

    public String 退出(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/pages/login.html";
    }
    @RequestMapping("/del")
    public String del(int aid){
        System.out.println(aid);
        adminService.del(aid);
        return "redirect:/admin/findAll";
    }
}
